package com.lv.hechuang.utils;

import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.lv.hechuang.R;
import com.lv.hechuang.app.App;
import com.lv.hechuang.common.CommonBaseUrl;

import java.io.File;


/**
 * Created by Administrator on 2016/10/29.
 */
public class ImageLoad {

    private final static String imageUrl = CommonBaseUrl.IMAGE_URL;

    /**
     * 加载普通图片 传入默认错误图和占位图
     */
    public static void loadImage(String url, final ImageView iv_show) {
        loadNormalImageHandleUrl(url, iv_show, R.mipmap.ic_launcher);
    }

    /**
     * 加载头像 传入默认错误图和占位图
     */
    public static void loadHeader(String url, final ImageView iv_show) {
        loadCircleImageHandleUrl(url, iv_show, R.mipmap.ic_launcher);
    }

    /**
     * 加载圆形图片 处理网络图片
     */
    private static void loadCircleImageHandleUrl(String url, final ImageView iv_show, int errorAndPlaceHoldImage) {
        if (!TextUtils.isEmpty(url)) {
            if (url.startsWith("http")) {
                if (App.instance().getApplicationContext() != null) {
                    loadCircleImage(url, iv_show, errorAndPlaceHoldImage);
                }
            } else {
                if (App.instance().getApplicationContext() != null) {
                    loadCircleImage(imageUrl + handleImagePath(url), iv_show, errorAndPlaceHoldImage);
                }
            }
        }
    }

    /**
     * 加载圆形图片
     */
    public static void loadCircleImage(String url, final ImageView iv_show, int errorAndPlaceHoldImage) {
        RequestOptions requestOptions = new RequestOptions()
                .centerCrop()
                .error(errorAndPlaceHoldImage)
                .placeholder(errorAndPlaceHoldImage)
                .transforms(new CircleCrop());
        //加载圆角图片
        Glide.with(App.instance().getApplicationContext())
                .load(url)
                .apply(requestOptions)
                .into(iv_show);
    }

    /**
     * 加载普通网络图片 处理网络图片
     */
    private static void loadNormalImageHandleUrl(String url, final ImageView iv_show, int errorAndPlaceHoldImage) {
        if (!TextUtils.isEmpty(url)) {
            if (url.startsWith("http")) {
                if (App.instance().getApplicationContext() != null) {
                    loadNormalImage(url, iv_show, errorAndPlaceHoldImage);
                }
            } else {
                if (App.instance().getApplicationContext() != null) {
                    loadNormalImage(imageUrl + handleImagePath(url), iv_show, errorAndPlaceHoldImage);
                }
            }
        }
    }

    /**
     * 加载普通网络图片
     */
    public static void loadNormalImage(String url, final ImageView iv_show, int errorAndPlaceHoldImage) {
        RequestOptions requestOptions = new RequestOptions()
                .centerCrop()
                .error(errorAndPlaceHoldImage)
                .placeholder(errorAndPlaceHoldImage);
        //加载普通图片
        Glide.with(App.instance().getApplicationContext())
                .load(url)
                .apply(requestOptions)
                .into(iv_show);
    }

    /**
     *
     */
    public static void loadImageSend(String imagePath, ImageView imageView) {
        if (!TextUtils.isEmpty(imagePath)) {
            if (imagePath.startsWith("http")) {
                RequestOptions options = new RequestOptions();
                Glide.with(App.instance().getApplicationContext())
                        .load(imagePath)
                        .apply(options)
                        .into(imageView);
            } else {
                RequestOptions options = new RequestOptions();
                Glide.with(App.instance().getApplicationContext())
                        .load(imageUrl + handleImagePath(imagePath))
                        .apply(options)
                        .into(imageView);
            }
        } else {
            RequestOptions options = new RequestOptions();
            Glide.with(App.instance().getApplicationContext())
                    .load(R.mipmap.wechat_icon)
                    .apply(options)
                    .into(imageView);
        }
    }

    private static String handleImagePath(String imagePath) {
        if (imagePath == null) {
            imagePath = "";
        }
        String imagePathF = "";
        if (!imagePath.startsWith("/")) {
            imagePathF = "/" + imagePath;
        } else {
            imagePathF = imagePath;
        }
        return imagePathF;
    }


    public static void loadImageFile(String img, ImageView iv) {
        if (!TextUtils.isEmpty(img)) {
            Glide.with(App.instance().getApplicationContext()).load(new File(img)).into(iv);
        }
    }
}
